package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.*;
import cn.wzut.edut.dao.pojo.table.*;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;

/**
 * 角色功能管理
 */
@Repository
public class Role {
    @Autowired
    private SysUserRoleRelMapper sysUserRoleRelMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuRelMapper sysRoleMenuRelMapper;
    @Autowired
    Permission permission;
    /**
     * 查询角色
     * 输入角色id
     * @return 角色
     */
    public SysRole selectRole(String role_id){
        /**
         * q1：以角色id查找角色名
         */
        QueryWrapper<SysRole> q1 = new QueryWrapper<>();
        q1.in("id",role_id);
        return sysRoleMapper.selectOne(q1);
    }
    /**
     * 查询角色
     * 输入角色name
     * @return 角色
     */
    public SysRole selectRoleInName(String role_name){
        /**
         * q1：以角色名查找角色
         */
        QueryWrapper<SysRole> q1 = new QueryWrapper<>();
        q1.in("role_name",role_name);
        return sysRoleMapper.selectOne(q1);
    }
    /**
     * 查询用户角色
     * 输入用户id
     * @return 角色列表
     */
    public List<SysRole> selectRoles(String user_id){
        /**
         * 构造条件q1,q2
         * q1：以用户id来查找角色id
         */
        QueryWrapper<SysUserRoleRel> q1 = new QueryWrapper<>();
        q1.select("role_id").in("user_id",user_id);
        List<SysRole> roles = new LinkedList<>();
        List<SysUserRoleRel> sysUserRoleRels = sysUserRoleRelMapper.selectList(q1);
        for(int i = 0;i<sysUserRoleRels.size();i++){
            roles.add(selectRole(sysUserRoleRels.get(i).getRoleId()));
        }
        return roles;
    }
    /**
     * 按角色id查找用户角色表,仅返回角色列表
     * @return
     */
    public List<String> selectSysRoleOnlyMenu(String id){
        /**
         * queryWrapper：按角色id，查找用户角色关系表
         */
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("menu_id").in("role_id",id);
        List<String> list = new LinkedList<>();
        List<SysRoleMenuRel> list1 = sysRoleMenuRelMapper.selectList(queryWrapper);
        for(int i = 0;i<list1.size();i++){
            list.add(list1.get(i).getMenuId());
        }
        return list;
    }
    /**
     * 按照角色id删除角色用户关系表
     */
    public boolean delSysUserRoleRel(String id){
        /**
         * queryWrapper：按照角色id删除角色用户关系表
         */
        try {
            QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("role_id", id);
            sysUserRoleRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 按照角色id和用户id删除角色用户关系表
     */
    public boolean delSysUserRole(String user_id,String role_id){
        /**
         * queryWrapper：按照角色id和用户id删除角色用户关系表
         */
        try {
            QueryWrapper<SysUserRoleRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id", user_id).in("role_id",role_id);
            sysUserRoleRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 按照角色id和菜单id删除角色菜单关系表
     */
    public boolean delSysRoleMenu(String role_id,String menu_id){
        /**
         * queryWrapper：按照角色id和用户id删除角色用户关系表
         */
        try {
            QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("menu_id", menu_id).in("role_id",role_id);
            sysRoleMenuRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 按照角色id删除角色菜单表
     */
    public boolean delRoleMenuRel(String id){
        /**
         * queryWrapper：按照角色id删除角色菜单关系表
         */
        try {
            QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("role_id", id);
            sysRoleMenuRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 按照id删除角色表
     */
    public boolean delRole(String id){
        /**
         * queryWrapper：按照id删除角色表
         */
//        try {
            delRoleMenuRel(id);
            delSysUserRoleRel(id);
            permission.delRolePermissionInRoleId(id);
            QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", id);
            sysRoleMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * 添加角色菜单
     * 需要随便向pojo中输入一个id
     */
    public String insertSysRoleMenuRel(SysRoleMenuRel sysRoleMenuRel){
//        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            sysRoleMenuRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
            sysRoleMenuRelMapper.insert(sysRoleMenuRel);
//        }catch (Exception e){
//            return sysRoleMenuRel.getId();
//        }
        return sysRoleMenuRel.getId();
    }
//    /**
//     * 更新角色
//     */
//    public boolean updateSysRole(SysRole sysRole){
////        try {
//            QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
//            queryWrapper.in("id",sysRole.getId());
//            sysRoleMapper.update(sysRole,queryWrapper);
////        }catch (Exception e){
////            return false;
////        }
//        return true;
//    }

    /**
     * 查询角色数
     */
    public long countSysRole(){
        return sysRoleMapper.selectCount(null);
    }
    /**
     * 查询所有角色
     */
    public List<SysRole> selectAllSysRole(){
        return sysRoleMapper.selectList(null);
    }
    /**
     * 查询所有角色菜单表
     */
    public List<SysRoleMenuRel> selectAllSysUserRoleRel(){
        return sysRoleMenuRelMapper.selectList(null);
    }

    /**
     * 更新角色菜单
     */
    public boolean updateSysRoleMenuRel(SysRoleMenuRel sysRoleMenuRel){
        try {
            QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",sysRoleMenuRel.getId());
            sysRoleMenuRelMapper.update(sysRoleMenuRel,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 分页
     * 查询所有角色
     */
    public List<SysRole> selectAllSysRolePage(long current,long size){
        return sysRoleMapper.selectPage(new Page<SysRole>(current,size),null).getRecords();
    }
    @Autowired
    private SysRolePermissionRelMapper sysRolePermissionRelMapper;
    /**
     * 根据权限id获取权限赋予的角色
     */
    public List<SysRole> selectRoleInPermissionId(String permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("permission_id",permission_id);
        List<SysRolePermissionRel> list1 = sysRolePermissionRelMapper.selectList(queryWrapper);
        List<SysRole> list = new LinkedList<>();
        for (int i = 0;i<list1.size();i++){
            QueryWrapper<SysRole> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",list1.get(i).getRoleId());
            list.add(sysRoleMapper.selectOne(queryWrapper1));
        }

        return list;
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询角色信息
     */
    public List<SysRole> selectSysRole(String id,String role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_name),"role_name",role_name);
        return sysRoleMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色信息数量
     */
    public long countSysRole(String id,String role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_name),"role_name",role_name);
        return sysRoleMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色信息
     * 分页
     */
    public List<SysRole> selectSysRolePage(String id,String role_name,long current,long size){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_name),"role_name",role_name);
        return sysRoleMapper.selectPage(new Page<SysRole>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询角色信息
     * 列表查询
     */
    public List<SysRole> selectSysRoleList(List<String> id,List<String> role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_name!=null,"role_name",role_name);
        return sysRoleMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色信息数量
     * 列表查询
     */
    public long countSysRoleList(List<String> id,List<String> role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_name!=null,"role_name",role_name);
        return sysRoleMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色信息
     * 列表查询
     * 分页
     */
    public List<SysRole> selectSysRoleListPage(List<String> id,List<String> role_name,long current,long size){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_name!=null,"role_name",role_name);
        return sysRoleMapper.selectPage(new Page<SysRole>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询角色菜单信息
     */
    public List<SysRoleMenuRel> selectSysRoleMenu(String id,String role_id,String menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(menu_id),"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色菜单信息数量
     */
    public long countSysRoleMenu(String id,String role_id,String menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(menu_id),"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色菜单信息
     * 分页
     */
    public List<SysRoleMenuRel> selectSysRoleMenuPage(String id,String role_id,String menu_id,long current,long size){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(menu_id),"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectPage(new Page<SysRoleMenuRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询角色菜单信息
     * 列表查询
     */
    public List<SysRoleMenuRel> selectSysRoleMenuList(List<String> id,List<String> role_id,List<String> menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(menu_id!=null,"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色菜单信息数量
     * 列表查询
     */
    public long countSysRoleMenuList(List<String> id,List<String> role_id,List<String> menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(menu_id!=null,"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色菜单信息
     * 列表查询
     * 分页
     */
    public List<SysRoleMenuRel> selectSysRoleMenuListPage(List<String> id,List<String> role_id,List<String> menu_id,long current,long size){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(menu_id!=null,"menu_id",menu_id);
        return sysRoleMenuRelMapper.selectPage(new Page<SysRoleMenuRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询角色权限信息
     */
    public List<SysRolePermissionRel> selectSysRolePermission(String id,String role_id,String permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(permission_id),"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色权限信息数量
     */
    public long countSysRolePermission(String id,String role_id,String permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(permission_id),"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色权限信息
     * 分页
     */
    public List<SysRolePermissionRel> selectSysRolePermissionPage(String id,String role_id,String permission_id,long current,long size){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(permission_id),"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectPage(new Page<SysRolePermissionRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询角色权限信息
     * 列表查询
     */
    public List<SysRolePermissionRel> selectSysRolePermissionList(List<String> id,List<String> role_id,List<String> permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(permission_id!=null,"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询角色权限信息数量
     * 列表查询
     */
    public long countSysRolePermissionList(List<String> id,List<String> role_id,List<String> permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(permission_id!=null,"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectCount(queryWrapper);
    }
    /**
     * 查询角色权限信息
     * 列表查询
     * 分页
     */
    public List<SysRolePermissionRel> selectSysRolePermissionListPage(List<String> id,List<String> role_id,List<String> permission_id,long current,long size){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(permission_id!=null,"permission_id",permission_id);
        return sysRolePermissionRelMapper.selectPage(new Page<SysRolePermissionRel>(current,size),queryWrapper).getRecords();
    }
    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */

    /**
     * 删除角色信息
     */
    public boolean delSysRole(String id,String role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_name),"role_name",role_name);
        sysRoleMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除角色信息
     * 列表删除
     */
    public boolean delSysRoleList(List<String> id,List<String> role_name){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_name!=null,"role_name",role_name);
        sysRoleMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除角色菜单信息
     */
    public boolean delSysRoleMenu(String id,String role_id,String menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(menu_id),"menu_id",menu_id);
        sysRoleMenuRelMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除角色菜单信息
     * 列表删除
     */
    public boolean delSysRoleMenuList(List<String> id,List<String> role_id,List<String> menu_id){
        QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(menu_id!=null,"menu_id",menu_id);
        sysRoleMenuRelMapper.delete(queryWrapper);
        return true;
    }
    /**
     * 删除角色权限信息
     * 列表删除
     */
    public boolean delSysRolePermissionList(List<String> id,List<String> role_id,List<String> permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(role_id!=null,"role_id",role_id);
        queryWrapper.in(permission_id!=null,"permission_id",permission_id);
        sysRolePermissionRelMapper.delete(queryWrapper);
        return true;
    }

    /**
     * 删除角色权限信息
     */
    public boolean delSysRolePermission(String id,String role_id,String permission_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(role_id),"role_id",role_id);
        queryWrapper.in(!StringUtils.isEmpty(permission_id),"permission_id",permission_id);
        sysRolePermissionRelMapper.delete(queryWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * 更新组织信息
     */
    public boolean updateRole(String id,String name){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("role_name",name);
        sysRoleMapper.update(null,updateWrapper);
        return true;
    }

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * 添加角色菜单
     */
    public String insertSysRoleMenuRel(String role_id,String menu_id){
        SysRoleMenuRel sysRoleMenuRel = new SysRoleMenuRel();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysRoleMenuRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysRoleMenuRel.setMenuId(menu_id);
        sysRoleMenuRel.setRoleId(role_id);
        sysRoleMenuRelMapper.insert(sysRoleMenuRel);

        return sysRoleMenuRel.getId();
    }
    /**
     * 添加角色
     * 需要随便向pojo中输入一个id
     */
    public String insertSysRole(SysRole sysRole){
//        try {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysRole.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysRoleMapper.insert(sysRole);
//        }catch (Exception e){
//            return sysRole.getId();
//        }
        return sysRole.getId();
    }
    /**
     * 批量
     * 添加角色
     * 需要随便向pojo中输入一个id
     */
    public int insertSysRoleList(List<SysRole> sysRoles){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysRoles.size();i++){
            sysRoles.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysRoleMapper.insertBatchSomeColumn(sysRoles);
    }
    /**
     * 批量
     * 添加角色菜单
     * 需要随便向pojo中输入一个id
     */
    public int insertSysRoleMenuRelList(List<SysRoleMenuRel> sysRoleMenuRels){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysRoleMenuRels.size();i++){
            sysRoleMenuRels.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysRoleMenuRelMapper.insertBatchSomeColumn(sysRoleMenuRels);
    }

    /**
     * 添加角色权限
     */
    public String insertSysRolePermissionRel(String role_id,String permission_id){
        SysRolePermissionRel sysRolePermissionRel = new SysRolePermissionRel();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        sysRolePermissionRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
        sysRolePermissionRel.setPermissionId(permission_id);
        sysRolePermissionRel.setRoleId(role_id);
        sysRolePermissionRelMapper.insert(sysRolePermissionRel);
        return sysRolePermissionRel.getId();
    }
    /**
     * 批量
     * 添加角色权限
     * 需要随便向pojo中输入一个id
     */
    public int insertSysRolePermissionRelList(List<SysRolePermissionRel> sysRolePermissionRels){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
        for (int i=0;i<sysRolePermissionRels.size();i++){
            sysRolePermissionRels.get(i).setId(String.valueOf(snowflakeIdWorker.nextId()));
        }
        return sysRolePermissionRelMapper.insertBatchSomeColumn(sysRolePermissionRels);
    }
    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    @Autowired
    User login;

    /**
     * 级联删除角色
     * @param role_id
     * @return
     */
    public boolean delCascadeRole(String role_id){
        login.delSysUserRole(null,null,role_id);
        delSysRoleMenu(null,role_id,null);
        delSysRole(role_id,null);
        return true;
    }
    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */

    /**
     * 获取拥有此菜单的角色列表
     * @param menu_id
     * @return List<SysRole>
     */
    public List<SysRole> selectRoleListByMenu(String menu_id){
        List<SysRole> list = sysRoleMapper.selectJoinList(SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysRoleMenuRel.class, SysRoleMenuRel::getRoleId,SysRole::getId)
                        .eq(SysRoleMenuRel::getMenuId, menu_id));
        return list;
    }
    /**
     * 获取拥有此菜单的角色列表
     * @param permission_id
     * @return List<SysRole>
     */
    public List<SysRole> selectRoleListByPermission(String permission_id){
        List<SysRole> list = sysRoleMapper.selectJoinList(SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysRolePermissionRel.class, SysRolePermissionRel::getRoleId,SysRole::getId)
                        .eq(SysRolePermissionRel::getPermissionId,permission_id));
        return list;
    }
    /**
     * 分页
     * 获取拥有此菜单的角色列表
     * @param menu_id
     * @return List<SysRole>
     */
    public List<SysRole> selectRoleListByMenuPage(String menu_id,long current,long size){
        List<SysRole> list = sysRoleMapper.selectJoinPage(new Page<>(current, size),SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysRoleMenuRel.class, SysRoleMenuRel::getRoleId,SysRole::getId)
                        .eq(SysRoleMenuRel::getMenuId, menu_id)).getRecords();
        return list;
    }
    /**
     * 分页
     * 获取拥有此权限的角色列表
     * @param permission_id
     * @return List<SysRole>
     */
    public List<SysRole> selectRoleListByPermissionPage(String permission_id,long current,long size){
        List<SysRole> list = sysRoleMapper.selectJoinPage(new Page<>(current, size),SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysRolePermissionRel.class, SysRolePermissionRel::getRoleId,SysRole::getId)
                        .eq(SysRolePermissionRel::getPermissionId,permission_id)).getRecords();
        return list;
    }
    /**
     * 获取某个用户所赋予的角色
     * @param user_id
     * @return
     */
    public List<SysRole> selectRoleListByUser(String user_id){
        List<SysRole> list = sysRoleMapper.selectJoinList(SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysUserRoleRel.class, SysUserRoleRel::getRoleId,SysRole::getId)
                        .eq(SysUserRoleRel::getUserId,user_id));
        return list;
    }
    /**
     * 获取某个用户所赋予的角色
     * @param user_id
     * @return
     */
    public List<SysRole> selectRoleListByUserPage(String user_id,long current,long size){
        List<SysRole> list = sysRoleMapper.selectJoinPage(new Page<>(current, size),SysRole.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysRole.class)
                        .leftJoin(SysUserRoleRel.class, SysUserRoleRel::getRoleId,SysRole::getId)
                        .eq(SysUserRoleRel::getUserId,user_id)).getRecords();
        return list;
    }
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    /**
     * 根据用户id获取权限
     */
    public List<SysPermission> selectPermissionByUserId(String user_id){
        List<SysPermission> list = sysPermissionMapper.selectJoinList(SysPermission.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysPermission.class)
                        .leftJoin(SysRolePermissionRel.class, SysRolePermissionRel::getPermissionId,SysPermission::getId)
                        .leftJoin(SysUserRoleRel.class,SysUserRoleRel::getRoleId,SysRolePermissionRel::getRoleId)
                        .eq(SysUserRoleRel::getUserId,user_id));
        return list;
    }
    @Autowired
    private SysMenuMapper sysMenuMapper;
    /**
     * 根据用户id获取菜单
     */
    public List<SysMenu> selectMenuByUserId(String user_id){
        List<SysMenu> list = sysMenuMapper.selectJoinList(SysMenu.class,
                new MPJLambdaWrapper<>()
                        .selectAll(SysMenu.class)
                        .leftJoin(SysRoleMenuRel.class, SysRoleMenuRel::getMenuId,SysPermission::getId)
                        .leftJoin(SysUserRoleRel.class,SysUserRoleRel::getRoleId,SysRoleMenuRel::getRoleId)
                        .eq(SysUserRoleRel::getUserId,user_id));
        return list;
    }
}
